package com.fms.gui.templateCreator.components.date;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import com.fms.model.factory.parts.IDateSelector;

public class CalendarInformer implements ITimeInformer, IDateSelector
{
	List<ITimeListener> listeners = new ArrayList<ITimeListener>();
	private Calendar	calendar;
	
	public CalendarInformer()
	{
		this.calendar = Calendar.getInstance(TimeZone.getDefault());
	}
	
	public void addTimeListener(ITimeListener listener)
	{
		listeners.add(listener);
	}

	
	public int getHour()
	{
		return this.calendar.get(Calendar.HOUR_OF_DAY);
	}

	
	public int getMinute()
	{
		return this.calendar.get(Calendar.MINUTE);
	}

	
	public void setTime(int hour, int minute)
	{
		this.calendar.set(Calendar.HOUR_OF_DAY, hour);
		this.calendar.set(Calendar.MINUTE, minute);
		postNewTime();
		
	}

	
	
	public Calendar getTime()
	{
		return this.calendar;
	}

	public void addMinutes(int difference)
	{
		System.out.println("Adding: " + difference);
		System.out.println("Before: " + calendar.get(Calendar.HOUR) + " " + calendar.get(Calendar.MINUTE));
		this.calendar.add(Calendar.MINUTE, difference);
		System.out.println("After: " + calendar.get(Calendar.HOUR) + " " + calendar.get(Calendar.MINUTE));
		postNewTime();
		
	}
	
	private void postNewTime()
	{
		for(ITimeListener l : listeners)
		{
			l.newTimePosted(getMinute(), getHour());
		}
	}

	

}
